package com.uroad.lib.imageloader;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentActivity;

import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import jp.wasabeef.glide.transformations.CropCircleTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * Created by jiadong on 2016/11/2.
 * use glide in private way to load image
 */

public class GlideImageLoaderStrategy implements BaseImageLoaderStrategy {

    RequestManager requestManager;

    @Override
    public void loadImage(Context ctx, ImageLoader img) {
        requestManager = Glide.with(ctx);
        loadNormal(ctx, img);
    }

    @Override
    public void loadImage(Activity activity, ImageLoader img) {
        requestManager = Glide.with(activity);
        loadNormal(activity, img);
    }

    @Override
    public void loadImage(FragmentActivity activity, ImageLoader img) {
        requestManager = Glide.with(activity);
        loadNormal(activity, img);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void loadImage(Fragment fragment, ImageLoader img) {
        requestManager = Glide.with(fragment);
        loadNormal(fragment.getContext(), img);
    }

    @Override
    public void loadImage(android.support.v4.app.Fragment fragment, ImageLoader img) {
        requestManager = Glide.with(fragment);
        loadNormal(fragment.getContext(), img);
    }

    @Override
    public void clearDiskCache(Context context) {
        GlideCacheUtil.getInstance().clearDiskCache(context);
    }

    @Override
    public void clearMemoryCache(Context context) {
        GlideCacheUtil.getInstance().clearMemoryCache(context);
    }

    @Override
    public void clearAllCache(Context context) {
        GlideCacheUtil.getInstance().clearAllCache(context);
    }

    @Override
    public String getCacheSize(Context context) {
        return GlideCacheUtil.getInstance().getCacheSize(context);
    }

    /**
     * load image with Glide
     */
    private void loadNormal(Context ctx, ImageLoader img) {
        //如果质量是无图，则不执行加载动作
        if(img.getQuality() == ImageLoaderHelper.PIC_QUALITY_NO_PIC){
            img.getImgView().setImageResource(img.getError());
            return;
        }
        DrawableRequestBuilder drawableRequestBuilder = requestManager.load(img.getUrl()).error(img.getError()).
                placeholder(img.getPlaceHolder());
        if (img.isNeedCache()) {
            drawableRequestBuilder.diskCacheStrategy(DiskCacheStrategy.ALL);
        } else {
            drawableRequestBuilder.skipMemoryCache(true);
            drawableRequestBuilder.diskCacheStrategy(DiskCacheStrategy.NONE);
        }
        if (img.getTransform() == ImageLoaderHelper.Transform_CropCircle) {
            drawableRequestBuilder.bitmapTransform(new CropCircleTransformation(ctx));
        } else if (img.getTransform() == ImageLoaderHelper.Transform_RoundedCorners) {
            drawableRequestBuilder.bitmapTransform(new RoundedCornersTransformation(ctx, img.getRadius(), 0, RoundedCornersTransformation.CornerType.ALL));
        }
        if (img.getThumbnail() > 0) {
            drawableRequestBuilder.thumbnail(img.getThumbnail());
        }
        drawableRequestBuilder.into(img.getImgView());
    }

}
